package ControlLayer; 
import java.util.ArrayList;
import java.util.Iterator;
import ModelLayer.*;
/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{
    
    //declare instance variables to reference containers
    //passed as arguments from the main class
    private Kontakt k;
    private kontaktKontainer kk;
    //Address container is to be passed as parameter from the main controller
    public AddressCtr()
    {
        kk = kontaktKontainer.getInstance();
    }
   
    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
    public void getPerson(int id)
    { 
        Kontakt fKontakt = kk.findKontakt(id);
       if (fKontakt != null)
       {
        //this method is to search the container for a person
       //with an id-number equal to the parameter id
       //when person is found, the method is to call print-methods in the TUI layer
       System.out.println("person "  + fKontakt.getFnavn() + " " + fKontakt.getEnavn());
       System.out.println("Adresse " + fKontakt.getAdresse());
       System.out.println("Postnr "  + fKontakt.getZip());
       System.out.println("By "      + fKontakt.getCity());
       System.out.println("Phone "   + fKontakt.getPhone());
    }
    else{
        System.out.println("Person eksistere ikke");
    }
    }
    
    public void createPerson(String fnavn, String enavn, String adresse, String mail, int zip, String city, int phone)

    {   
         kk.createPerson(fnavn, enavn, adresse, mail, zip, city, phone);
    }
    
    public void deletePerson(int id)
    {
        boolean deleted = false;
        while(deleted==false){
            if(kk.findKontakt(id)!=null){
                kk.removeKontakt(id);
                deleted = true;
            }
        }
    }
    
    public void updatePerson(int id, String fnavn, String enavn, String adresse, String mail, int zip, String city, int phone)
    {
           if(kk.findKontakt(id) != null) {
               kk.findKontakt(id).updatePerson(fnavn, enavn, adresse, mail, zip, city, phone);
               System.out.println("Persondata opdateret");
           }
           else{
               System.out.println("Person ikke fundet");
           }
    }    
     
    public ArrayList<Kontakt> listAllPersons()
    {
          return kk.getAllPersons();
    }
}
